package com.dqyt.app.service;


import com.alibaba.fastjson.JSON;
import com.dqyt.app.bean.Report;
import com.dqyt.app.bean.Sample;
import com.dqyt.app.bean.WorkBean;
import com.dqyt.app.mongo.ReportDao;
import com.dqyt.app.mongo.SampleDao;
import com.dqyt.common.check.CheckTool;
import com.dqyt.common.constant.AuthorityID;
import com.dqyt.common.constant.Opt;
import com.dqyt.common.utils.BHUtil;
import com.dqyt.common.utils.StringUtil;
import com.dqyt.common.utils.UserInfoUtil;
import com.dqyt.khjc.bean.*;
import com.dqyt.khjc.mapper.*;
import com.dqyt.khjc.swicth.SwitchConfig;
import com.dqyt.khjc.utils.IpUtil;
import com.dqyt.khjc.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

/**
 * 样品service
 */
@Service
public class SampleService {

    @Autowired
    public  SampleDao mSampleDao2;

    @Autowired
    private QrCodeMapper qrCodeMapper;

    @Autowired
    private WorkMapper workMapper;


    @Autowired
    private CustomerMapper customerMapper;

    @Autowired
    private ManufacturerMapper manufacturerMapper;

    @Autowired
    private ReportDao reportDao;


    @Autowired
    private HistoryMapper historyMapper;


    /**
     * 查询样品列表信息分页
     *
     * @param request
     * @param addTime
     * @param type
     * @param bsbh
     * @param erweima
     * @return
     */
    public ResultMsg getSampleInfos(HttpServletRequest request,Integer page,Integer size, long userId, String token, long addTime,
                                    int type, String bsbh, String erweima,long wtfId,int sampleStatue) {

        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {

            // 简单的数据检查
            if (addTime == 0) {
                ret.setCode(ResultMsg.FAIL);
                ret.setMsg("数据错误！");
                return ret;
            }

            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();

            if (page != null && size != null) {
                page = (page - 1) * size;
            }
            // 取得条件数据
            List<Sample> samples = mSampleDao2.getSampleInfos3(page,size,addTime, type, bsbh, erweima,wtfId,sampleStatue);

            ret.setCode(ResultMsg.SUCESS);
            ret.setData(JSON.toJSONString(samples));
            return ret;

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 查询样品信息
     *
     * @param request
     * @param userId
     * @param token
     * @param type
     * @param bsbh
     * @param erweima
     * @return
     */
    public ResultMsg getSample(HttpServletRequest request, long userId, String token, int type, String bsbh, String erweima,String sbbh) {

        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {

            // 简单的数据检查
            if (type == 0) {
                ret.setCode(ResultMsg.FAIL);
                ret.setMsg("数据错误！");
                return ret;
            }

            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();

            //通过标识编号去查询
            Sample sample33 =   mSampleDao2.getPcSampleByBsbh(bsbh);
            if(null != sample33){
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("该标识编号已经被使用了");
                return ret;
            }
            ret.setCode(ResultMsg.NEW_SAMPLE_FP);
            return ret;

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 扫描二维码查询样品信息
     *
     * @param request
     * @param userId
     * @param token
     * @param erweima
     * @return
     */
    public ResultMsg getSampleByQrcode(HttpServletRequest request, long userId, String token, String erweima) {

        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();

            //通过标识编号去查询
            Sample sample33 =   mSampleDao2.getSampleByQrcode(erweima);
            if(null != sample33){
                ret.setCode(ResultMsg.SUCESS);
                ret.setData(sample33);
                ret.setMsg("查询成功");
                return ret;
            }else{
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("无数据");
                return ret;
            }
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 查询样品信息
     *
     * @param request
     * @param userId
     * @param token
     * @param type
     * @return
     */
    public ResultMsg getSampleBysbbh(HttpServletRequest request, long userId, String token, int type, String sbbh) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 简单的数据检查
            if (type == 0) {
                ret.setCode(ResultMsg.FAIL);
                ret.setMsg("数据错误！");
                return ret;
            }
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();

            // 取得条件数据
            List<Sample> samples = mSampleDao2.getSample(type,sbbh, "", "");

            Sample sample = null;

            if(samples.size()>0){
                sample = samples.get(0); // 取得数据
                //在根据旧的数据吧二维码查询出来
                QrCodeBean qrCodeBean =  qrCodeMapper.getQrCodeBySbbh(sample.sbbh);
                if(null !=qrCodeBean){
                    sample.erweima =qrCodeBean.getErweima();
                    if( sample.type == 1){//如果是呼吸器
                        sample.respirator.erweima = qrCodeBean.getErweima();
                    }else{
                        sample.cylinders.erweima = qrCodeBean.getErweima();
                    }
                    ret.setCode(ResultMsg.OLD_SAMPLE); // 旧的数据
                    ret.setData(sample);
                }else{
                    ret.setCode(ResultMsg.ERROR); // 旧的数据
                    ret.setMsg("二维码生成异常，请刷新重试");
                }
            }else{
                //就创建二维码 并将二维码保存到本地
                ret.setCode(ResultMsg.NEW_SAMPLE_FP);
                sample = new Sample(); // 新建数据
                sample.type = type;
                sample.erweima = BHUtil.getErWeiMa(type,sbbh);
                ret.setData(sample);
                QrCodeBean qrCodeBean =null;
                qrCodeBean =  qrCodeMapper.getQrCodeBySbbh(sbbh);
                if(qrCodeBean == null){
                    qrCodeBean = new QrCodeBean();
                    //将二维码放到数据库
                    qrCodeBean.setSbbh(sbbh);
                    qrCodeBean.setErweima(sample.erweima);
                    long nowTime = TimeUtil.getCurrentTimeMillis();
                    qrCodeBean.setAddTime(nowTime);
                    qrCodeBean.setAddUser(userId+"");
                    qrCodeBean.setUpdateTime(nowTime);
                    qrCodeBean.setUpdateUser(userId+"");
                    qrCodeMapper.saveQrCode(qrCodeBean);
                }
            }
            ret.setData(JSON.toJSONString(sample));
            return ret;

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 查询 匹配气瓶信息
     * @param request
     * @param userId
     * @param token
     * @param bsbh
     * @return
     */
    public ResultMsg  getCylindersBysbbh(HttpServletRequest request, long userId, String token, String bsbh){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();

            // 取得条件数据
            List<Sample> samples = mSampleDao2.getSample(2,"", bsbh, "");
            if(samples.size()>0){
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("查询成");
                ret.setData(samples.get(0));
            }else{
                ret.setCode(ResultMsg.SUCESS);
                ret.setMsg("查询成");
                ret.setData(null);
            }
        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }



    /**
     * 新增样品信息
     *
     * @param request
     * @param userId
     * @param token
     * @param json
     * @return
     */
    public ResultMsg addSample(HttpServletRequest request, long userId, String token, String json) {
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 简单的数据检查
            if (json == null || "".equals(json)) {
                ret.setCode(ResultMsg.FAIL);
                ret.setMsg("数据错误！");
                return ret;
            }
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }

           User user =  UserInfoUtil.getUserInfologin(userId, token);

            ret = new ResultMsg();
            Sample sample = JSON.parseObject(json, Sample.class);
            sample.sbbh = sample.sbbh.trim();

            // 数据检查
            if (StringUtil.isEmpty(sample.bsbh) || StringUtil.isEmpty(sample.erweima)) {
                ret.setCode(ResultMsg.FAIL);
                ret.setMsg("数据错误！");
                return ret;
            }

            //设置上次检验日期
            //先查询最新的数据
            List<Sample> mmsmps =  mSampleDao2.getSample(sample.type,sample.sbbh, "", "");
            if(null != mmsmps && mmsmps.size()>0){
                Sample oll = mmsmps.get(0);
                //如果是呼吸器
                if(sample.type == Sample.YP_TYPE_HXQ){
                    if(!sample.bsbh.equals(oll.bsbh)){
                        sample.respirator.scjyrq = oll.respirator.sjri;
                    }

                }else{//气瓶
                    if(!sample.bsbh.equals(oll.bsbh)){
                        sample.cylinders.scjyrq = oll.cylinders.sjri;
                    }
                }
            }


            //如果面罩对象不为空
            if(null != sample.respirator){
                //如果委托方Id为空就新建一条委托方的数据
                if(0 == sample.respirator.wtfId || "".equals(sample.respirator.wtfId)){
                    CustomerBean customerBean = new CustomerBean();
                    customerBean.setEnterpriseName(sample.respirator.wtf);
                    customerBean.setProvince(0);
                    customerBean.setCity(0);
                    customerBean.setBlock(0);
                    customerBean.setAddress("");
                    customerBean.setContacts("");
                    customerBean.setPhone("");
                    customerBean.setEmail("");
                    customerBean.setTelephone("");
                    customerBean.setFax("");
                    customerBean.setAddTime(TimeUtil.currentTimeMillis());
                    customerBean.setAddUser(String.valueOf(user.getUserId()));
                    customerBean.setUpdateTime(TimeUtil.currentTimeMillis());
                    customerBean.setUpdateUser(String.valueOf(user.getUserId()));
                    int res = customerMapper.addCustomer(customerBean);
                    sample.wtfId = customerBean.getCustomerId();
                    sample.sjr = sample.respirator.sjr;
                }else{
                    sample.wtfId = sample.respirator.wtfId;
                    sample.sjr = sample.respirator.sjr;
                    sample.sjrId =sample.respirator.sjrId;
                }

                //如果制造厂商的id为空  就新增一条信息
                if(null ==sample.respirator.zzcsId|| "".equals(sample.respirator.zzcsId)){
                    ManufacturerBean manufacturerBean = new ManufacturerBean();
                    manufacturerBean.setName(sample.respirator.zzcs);
                    manufacturerBean.setEnname("");
                    manufacturerBean.setAddress("");
                    long tt =TimeUtil.currentTimeMillis();
                    manufacturerBean.setAddTime(tt);
                    manufacturerBean.setAddUser(user.getUserId()+"");
                    manufacturerBean.setUpdateTime(tt);
                    manufacturerBean.setUpdateUser(user.getUserId()+"");
                    //新增制造厂商
                    manufacturerMapper.addManufacturers(manufacturerBean);
                    sample.zzcsId = String.valueOf(manufacturerBean.getId());
                    sample.zzcs = sample.respirator.zzcs;
                }else{
                    sample.zzcsId = sample.respirator.zzcsId;
                    sample.zzcs = sample.respirator.zzcs;
                }


                String sjrqstr = sample.respirator.sjri;


                SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy");
                SimpleDateFormat dateFormat2 = new SimpleDateFormat("yyyy.MM");
                //输出结果为
                String jyrqNianStr =   dateFormat.format(TimeUtil.parsex(sjrqstr));
                //输出结果为
                String jyrqYueStr =   dateFormat2.format(TimeUtil.parsex(sjrqstr));
                sample.jyrqNian = jyrqNianStr;
                sample.jyrqYue = jyrqYueStr;




                //-------修改了匹配气瓶的信息  对相应的报告的疲惫信息做更改start ------------//
                //先查询对应的报告有没有 (面罩)
                Report isReport =  reportDao.getReportBybsbh(sample.bsbh,1);
                //如果查询出来有报告
                if(null !=isReport){
                    //如果两个的匹配气瓶信息不一样
                    //if(!sample.respirator.qpbsh.equals(isReport.respirator.qpbsh)){
                    //将新的匹配气瓶 设置过去
                    isReport.respirator.qpbsh = sample.respirator.qpbsh;
                    reportDao.save(isReport);
                    //}
                }
                //-------修改了匹配气瓶的信息  对相应的报告的疲惫信息做更改end ------------//


                //计算上次检验日期
                List<Sample> samplesoo =   mSampleDao2.getSample(1,sample.sbbh,"","");
                if(samplesoo.size()>0){
                    Sample sample122 = samplesoo.get(0);
                    if(sample122.bsbh != sample.bsbh){
                        sample.respirator.scjyrq = sample122.respirator.sjri;
                    }
                }
            }else if(null != sample.cylinders){
                //如果委托方Id为空就新建一条委托方的数据
                if( 0  == sample.cylinders.wtfId || "".equals(sample.cylinders.wtfId)){
                    CustomerBean customerBean = new CustomerBean();
                    customerBean.setEnterpriseName(sample.cylinders.wtf);
                    customerBean.setProvince(0);
                    customerBean.setCity(0);
                    customerBean.setBlock(0);
                    customerBean.setAddress("");
                    customerBean.setContacts("");
                    customerBean.setPhone("");
                    customerBean.setEmail("");
                    customerBean.setTelephone("");
                    customerBean.setFax("");
                    customerBean.setAddTime(TimeUtil.currentTimeMillis());
                    customerBean.setAddUser(String.valueOf(user.getUserId()));
                    customerBean.setUpdateTime(TimeUtil.currentTimeMillis());
                    customerBean.setUpdateUser(String.valueOf(user.getUserId()));
                    int res = customerMapper.addCustomer(customerBean);
                    sample.wtfId =customerBean.getCustomerId();
                    sample.sjr = sample.cylinders.sjr;
                }else{
                    sample.wtfId = sample.cylinders.wtfId;
                    sample.sjr = sample.cylinders.sjr;
                    sample.sjrId = sample.cylinders.sjrId;
                }

                //如果制造厂商的id为空  就新增一条信息
                if(null ==sample.cylinders.zzcsId|| "".equals(sample.cylinders.zzcsId)){
                    ManufacturerBean manufacturerBean = new ManufacturerBean();
                    manufacturerBean.setName(sample.cylinders.zzcs);
                    manufacturerBean.setEnname("");
                    manufacturerBean.setAddress("");
                    long tt =TimeUtil.currentTimeMillis();
                    manufacturerBean.setAddTime(tt);
                    manufacturerBean.setAddUser(user.getUserId()+"");
                    manufacturerBean.setUpdateTime(tt);
                    manufacturerBean.setUpdateUser(user.getUserId()+"");
                    //新增制造厂商
                    manufacturerMapper.addManufacturers(manufacturerBean);
                    sample.zzcsId = String.valueOf(manufacturerBean.getId());
                    sample.zzcs = sample.cylinders.zzcs;
                }else{
                    sample.zzcsId = sample.cylinders.zzcsId;
                    sample.zzcs = sample.cylinders.zzcs;
                }

                String sjrqstr = sample.cylinders.sjri;
                SimpleDateFormat dateFormat4= new SimpleDateFormat("yyyy");
                SimpleDateFormat dateFormat5 = new SimpleDateFormat("yyyy.MM");
                //输出结果为
                String jyrqNianStr =   dateFormat4.format(TimeUtil.parsex(sjrqstr));
                //输出结果为
                String jyrqYueStr =   dateFormat5.format(TimeUtil.parsex(sjrqstr));
                sample.jyrqNian = jyrqNianStr;
                sample.jyrqYue = jyrqYueStr;

                //计算上次检验日期
                List<Sample> samplesoo =   mSampleDao2.getSample(2,sample.sbbh,"","");
                if(samplesoo.size()>1){
                    Sample sample122 = samplesoo.get(1);
                    if(sample122.bsbh != sample.bsbh){
                        sample.cylinders.scjyrq = sample122.cylinders.sjri;
                    }
                }
            }

            // 保存数据
            // 取得是否存在旧的数据
            Sample oldData = mSampleDao2.getSampleByBH(sample.bsbh);
            if (oldData != null) {
                sample.sampleStatue = oldData.sampleStatue; // 样品状态记录
                sample.bhgyy = oldData.bhgyy;
                sample.rwdbh= oldData.rwdbh;
                sample.jsrqLong = oldData.jsrqLong;

                //如果是面罩
                if(oldData.type ==Sample.YP_TYPE_HXQ){
                    sample.respirator.zjqmx = oldData.respirator.zjqmx;
                    sample.respirator.bjsq = oldData.respirator.bjsq;
                    //sample.respirator.sfxp = oldData.respirator.sfxp;
                    sample.respirator.xjpType = oldData.respirator.xjpType;
                    sample.respirator.zsjyrq = oldData.respirator.zsjyrq;
                }else{
                    sample.cylinders.zsjyrq = oldData.cylinders.zsjyrq;
                }
            }else{
                sample.sampleStatue = 2;  // 没有已接收状态了  来就是检测中
            }

            // 设置一次数据编号
            sample.bh = sample.bsbh;//BHUtil.getBHID(sample.type, sample.bsbh);
            long time = TimeUtil.getCurrentTimeMillis();


            sample.setAddUser(user.getUserId() + "");
            sample.setAddTime(time);
            sample.setUpdateUser(user.getUserId() + "");
            sample.setUpdateTime(time);
            sample.setSysStatus(BaseBean.SYS_STATUS_OK);

            if(sample.type ==1){
                //如果没有分配任务
                if(null ==sample.respirator.jcrId || "".equals(sample.respirator.jcrId)){
                    //查询任务列表
                    WorkBean workBean = workMapper.getWorkListByBsbh(sample.bsbh);
                    if(null !=workBean){
                        if(workBean.getJyyId() != user.getUserId()){
                            ret.setCode(ResultMsg.ERROR);
                            ret.setMsg("不是您的任务,请查看标识编号是否输入正确！");
                            return ret;
                        }

                        sample.respirator.jcrId = workBean.getJyyId()+"";
                        sample.respirator.jcr = workBean.getJyy();
                        sample.rwdbh = workBean.getRwdbh();
                        sample.jsrqLong = workBean.getJsrqLong();
                        //并更新工作表的 状态和 设备编号
                        workMapper.upfateWorkBhOrTp(sample.bsbh,sample.sbbh,3);
                    }
                }
            }else{
                //如果没有分配任务
                if(null ==sample.cylinders.jcrId || "".equals(sample.cylinders.jcrId)){
                    //查询任务列表
                    WorkBean workBean = workMapper.getWorkListByBsbh(sample.bsbh);
                    if(null !=workBean){
                        if(workBean.getJyyId() != user.getUserId()){
                            ret.setCode(ResultMsg.ERROR);
                            ret.setMsg("不是您的任务,请查看标识编号是否输入正确！");
                            return ret;
                        }
                        if(workBean.getJyyId() != user.getUserId()){
                            ret.setCode(ResultMsg.ERROR);
                            ret.setMsg("不是您的任务,请查看标识编号是否输入正确！");
                            return ret;
                        }
                        sample.cylinders.jcrId =workBean.getJyyId()+"";
                        sample.cylinders.jcr = workBean.getJyy();
                        sample.rwdbh = workBean.getRwdbh();
                        sample.jsrqLong = workBean.getJsrqLong();
                        //并更新工作表的 状态和 设备编号
                        workMapper.upfateWorkBhOrTp(sample.bsbh,sample.sbbh,3);
                    }
                }
            }

            if(sample.type == Sample.YP_TYPE_HXQ){
                String pxzdStr = sample.bsbh.replace("DQFJ","");
                sample.pxzd = pxzdStr;
            }else{
                String pxzdStr = sample.bsbh.replace("YTQP","");
                sample.pxzd = pxzdStr;
            }

            mSampleDao2.save(sample);
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("保存成功");

            //如果操作日志的开关已经打开
            if(SwitchConfig.isOpen(SwitchConfig.ID_CZRZ)){
                //新增操作记录
                // 先取得用户ip
                String ip = IpUtil.getIpAddr(request);
                HistoryBean historyBean = new HistoryBean();
                if(sample.type == 1){
                    historyBean.setMessage("[" + user.getUserName() + "] 新增了编号为： [" + sample.bh + "] 的样品 [呼吸器]");
                    historyBean.setType(Opt.YPGL_YPDJ_XZYP_XZHXQ);
                }else{
                    historyBean.setMessage("[" + user.getUserName() + "] 新增了编号为： [" + sample.bh + "] 的样品 [复合气瓶]");
                    historyBean.setType(Opt.YPGL_YPDJ_XZYP_FHQP);
                }
                historyBean.setOpUserId(user.getUserId());
                historyBean.setIp(ip);
                historyBean.setBopId(String.valueOf(sample.bh));
                historyBean.setAddTime(TimeUtil.currentTimeMillis());
                historyBean.setAddUser(String.valueOf(user.getUserId()));
                historyBean.setUpdateTime(TimeUtil.currentTimeMillis());
                historyBean.setUpdateUser(String.valueOf(user.getUserId()));
                //插入数据库
                int ress = historyMapper.addHistory(historyBean);
            }

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }

    /**
     * 更新样品信息
     *
     * @param request
     * @param userId
     * @param token
     * @param json
     * @return
     */
    public ResultMsg updateSample(HttpServletRequest request, long userId, String token, String json) {

        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {

            // 简单的数据检查
            if (json == null || "".equals(json)) {
                ret.setCode(ResultMsg.FAIL);
                ret.setMsg("数据错误！");
                return ret;
            }

            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg();

            Sample sample = JSON.parseObject(json, Sample.class);
            // 数据检查
            if (StringUtil.isEmpty(sample.bh) || StringUtil.isEmpty(sample.bsbh) || StringUtil.isEmpty(sample.erweima)) {
                ret.setCode(ResultMsg.FAIL);
                ret.setMsg("数据错误！");
                return ret;
            }

            // 保存数据
            Sample oldData = mSampleDao2.getSampleByBH(sample.bh);
            if (oldData == null) {
                ret.setCode(ResultMsg.FAIL);
                ret.setMsg("数据错误！");
                return ret;
            }else{
                if (oldData != null) {
                    sample.sampleStatue = oldData.sampleStatue; // 样品状态记录
                    sample.bhgyy = oldData.bhgyy;
                    sample.rwdbh= oldData.rwdbh;
                    sample.jsrqLong = oldData.jsrqLong;
                    sample.pxzd=oldData.pxzd;
                    //如果是面罩
                    if(oldData.type ==Sample.YP_TYPE_HXQ){
                        sample.respirator.zjqmx = oldData.respirator.zjqmx;
                        sample.respirator.bjsq = oldData.respirator.bjsq;
                        //sample.respirator.sfxp = oldData.respirator.sfxp;
                        sample.respirator.xjpType = oldData.respirator.xjpType;
                        sample.respirator.zsjyrq = oldData.respirator.zsjyrq;
                    }else{
                        sample.cylinders.zsjyrq = oldData.cylinders.zsjyrq;
                    }
                }else{
                    sample.sampleStatue = 2;  // 没有已接收状态了  来就是检测中
                }
            }

            long time = TimeUtil.getCurrentTimeMillis();
            sample.setUpdateUser(userId + "");
            sample.setUpdateTime(time);
            sample.setSysStatus(BaseBean.SYS_STATUS_OK);

            mSampleDao2.save(sample);

            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("更新数据成功");
            ret.setData(JSON.toJSONString(sample));

        } catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }


    /**
     * 查询为分配的 任务的 样品
     * @param bsbh   标识编号
     * @param type  样品类型  1：呼吸器  2: 复合气瓶
     * @return
     */
    public ResultMsg getSampleListWorkAll(HttpServletRequest request,long userId, String token,String bsbh,int type){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 权限检查
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值
            //查询所有数据
            List<Sample>  samples =  mSampleDao2.getSampleListWorkAll( bsbh, type);
            ret.setData(samples);
            ret.setCode(ResultMsg.SUCESS);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;

    };



    /**
     * 分页查询 任务分配
     * @param request
     * @param page
     * @param size
     * @param rwdbh
     * @param mtype
     * @return
     */
    public ResultMsg getWorkListPage(HttpServletRequest request,Integer page,Integer size,long userId,
                                     String token,String rwdbh,long wtfId,int mtype,int workTYpe){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 检查用户是否登录Token是否正确
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            // 如果有错误信息
            if (ret != null) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值

            if (page != null && size != null) {
                page = (page - 1) * size;
            }
            int  workTYpeCh=0;
            int workTYpeCh2=0;

            if(workTYpe==88){
                workTYpe = 4;
                workTYpeCh = 1;
                workTYpeCh2=0;
            }else if(workTYpe==99){
                workTYpe = 4;
                workTYpeCh2=1;
                workTYpeCh=0;
            }
            List<WorkBean> workBeans =  workMapper.getAdWorkListPage( page, size, rwdbh, wtfId, mtype, workTYpe, workTYpeCh, workTYpeCh2);
            ret.setCode(ResultMsg.SUCESS);
            ret.setData(workBeans);
        }catch (Exception e) {
            ret.setCode(ResultMsg.ERROR);
            ret.setMsg("未知的错误，请稍候再试！");
            e.printStackTrace();
        }
        return ret;
    }




    /**
     *
     * @param request
     * @param userId
     * @param token
     * @param bsbhArrar
     * @param jcrUserId
     * @param jcrStr
     * @return
     */
    public ResultMsg assignTasks(HttpServletRequest request,long userId, String token,String  bsbhArrar,String jcrUserId,String jcrStr){
        ResultMsg ret = new ResultMsg(); // 声明一个返回值
        try {
            // 权限检查
            ret = UserInfoUtil.isAuthori_App(userId, token); // 验证用户是否登录
            if (null != ret) {
                return ret;
            }
            ret = new ResultMsg(); // 声明一个返回值

            if("".equals(bsbhArrar)){
                ret.setCode(ResultMsg.FAIL);
                ret.setMsg("非法操作！");
                return ret;
            }
            String  [] bsbhArrar2 = bsbhArrar.split(",");
            for(String bsbh:bsbhArrar2){
                //
                Sample mSample =  mSampleDao2.getPcSampleByBsbh(bsbh);
                if(mSample.type == 1){
                    mSample.respirator.jcrId = jcrUserId;
                    mSample.respirator.jcr =jcrStr;
                }else{
                    mSample.cylinders.jcrId = jcrUserId;
                    mSample.cylinders.jcr =jcrStr;
                }
                mSampleDao2.save(mSample);
            }
            ret.setCode(ResultMsg.SUCESS);
            ret.setMsg("分配成功");

        }catch (Exception e) {
                ret.setCode(ResultMsg.ERROR);
                ret.setMsg("未知的错误，请稍候再试！");
                e.printStackTrace();
            }
            return ret;
    }


}
